/**
 * @todo Create sub-types for finder
 * @todo Pretend that the DOM object doesn't exist, ever, then add it as a special happy happy for the user,
  * since we won't have it available when we save and reload a TestSet. 
 *
 */

Actionable = function( parent, cmdName, args )
{

    this.doInit(parent, cmdName, args);
    this.tasksList = parent;
    this.upDownEnabled = true;

}

Actionable.prototype = domplate(TestFireObject, {


    _cmd: null,
    targetObject: null,
    htmlFinder: null,
    parent: null,
    finderString: null,
    deletable: 1,
    args: {},


    init: function(parent, cmdName, args)
    {
        this.parent = parent;
        this.displayValue = cmdName;
        this._cmd = cmdName;
        this.args = args ? args : {};
    },

    populate: function( parent, targetObject, htmlFinder, finderName )
    {
        return this.doPopulate.apply(this, arguments);
    },


    doInit: function(  )
    {
        this.init.apply(this, arguments);
    },

    isUpAvailable : function()
    {
        return this._isUpAvailable(this.tasksList.tasks);

    },

    isDownAvailable : function()
    {
        return this._isDownAvailable(this.tasksList.tasks);
    },

    moveDown : function()
    {
        return this._moveDown(this.tasksList.tasks);
    },

    moveUp : function()
    {
        return this._moveUp(this.tasksList.tasks);
    },


/**
 * Get all displayable children and their names in an array of objects. The returned values will be displayed
 * in the order returned.
 * @return An array containing objects of the form: {name: 'name', object: this[name]} .
 */
    getChildrenToDisplay: function()
    {
        var ary = new Array();
        if ( this.targetObject )
        {
            ary.push({name: "Target Object" , object: this.targetObject});
        }
        else if(this.targetObjectString)
        {
            ary.push({name: "Target Object" , object: this.targetObjectString});
        }
        if(this.value){
            ary.push({name: "Value", object: this.value });
        }

        return ary;
    },


/**
 * A simple default.
 * @return if this has displayable children or not.
 */
    getHasChildren: function()
    {
        return this.getChildrenToDisplay().length != 0;
    },

    open : function(element, valueElement)
    {
        var serializer = new XMLSerializer();
        var temp  = serializer.serializeToString(element);
        var idx = temp.indexOf('xmlns:tf="TestFire"');
        this.finderString = temp.substring(0, idx);
        this.finderString = this.finderString + temp.substring(idx + 20);
        var finder = this.getFirstChildElement(element);
        var finderName =  finder.localName;
        finderName = finderName.charAt(0).toUpperCase() + finderName.substring(1);
        if(window[finderName]){
            this.htmlFinder = window[finderName];
        }
        element = this.getLastChildElement(element);
        if(valueElement)
        {
            this.value = valueElement.getAttribute('value');
        }
        var finderType = this.getFirstChildElement(finder);
        if(!finderType)
        {
            return;
        }
        finderType = this.getFirstChildElement(finderType);
        var val = finderType.getAttribute('value');
        if(val)
        {
            if(val.length > 20)
            {
                val = val.substring(0, 17) + '...';
            }
        }
        this.targetObjectString =
            finderName + " ('" + finderType.localName + "'='" + val + "')";
    },

    doToXMLStringAry: function( attributes )
    {
        var ary = new Array();
        if ( this.finderString)
        {
            ary = ary.concat(this.finderString);
        }
        return ary;
    },

    toXMLStringAry: function( nestedEls )
    {
        return this.doToXMLStringAry();
    },

    toString : function()
    {
        var text = "";
        var val = "";

        text = this.htmlFinder ? this.htmlFinder._type : this.displayValue;
        val = this.value;

        if(val)
        {
            if(val.length > 20)
            {
                val = val.substring(0, 17) + '...';
            }
            text += ": '" + val + "'";
        }
        return text;
    },

    doPopulate: function( parent, targetNode, htmlFinder, finderName )
    {
        try
        {
            this.parent = parent;
            this.htmlFinder = htmlFinder;
            this.targetObject = targetNode;
            this.finderString = FinderBuilder.build(targetNode, htmlFinder, finderName, null, this.args);
            this.value = this.targetObject.value;
            if(this.finderString == null){
                    alert("Action aborted");
                    return false;
            }
            return true;
            
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

    },

    deleteSelf: function(){
        this.parent.tasks = this.deleteFromArray(this, this.parent.tasks);
        Firebug.TestFire.refreshAll();
    }





});

